package main

import (
	"crypto/hmac"
	"crypto/sha256"
	"fmt"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
	"net/url"
	"path/filepath"
	"sort"
	"strings"
	"time"
)

const APP_ID = "bjofTVMomCQt0G2zTUhFVEs6SqDfq3zC"

const APP_KEY = "t4PcEhiG9ktlzzFQzKWM17UZWrbiXkJm"

func main() {
	//SendMsg()
	logPath := "/data/logs/gf-api/app.log"
	dirName, _ := filepath.Abs(filepath.Dir(logPath))
	fmt.Println(dirName)
}

func SendMsg() {
	param := map[string]interface{}{}
	param["app_id"] = APP_ID
	param["ts"] = time.Now().Unix()
	param["uid"] = "jiangj,yuanlj"
	param["title"] = "要发送的消息标题"
	param["msg"] = "要发送的消息内容"
	param["rtx_msg"] = "要发送的rtx消息内容"
	param["need_rtx"] = 1
	param["need_oa_msg"] = 0
	param["need_gt"] = 0
	param["need_mail"] = 0
	param["sign"] = MakeSign(param)

	cli := ghttp.Client{}
	requestUrl := "http://192.168.90.206/api/v1/msg/sendMsg"
	r, err := cli.Post(requestUrl, param)
	if err != nil {
		panic(err)
	}
	fmt.Println(r.ReadAllString())
}

func MakeSign(param map[string]interface{}) string {
	slice := []string{}
	for k, _ := range param {
		slice = append(slice, k)
	}
	sort.Strings(slice)

	str := ""
	for _, v := range (slice) {
		str += v + "=" + url.PathEscape(gconv.String(param[v])) + "&"
	}
	str = strings.Trim(str, "&")
	sign := HmacSha256(str)
	return string(sign)
}

func HmacSha256(param string) string {
	var key = []byte(APP_KEY)

	h := hmac.New(sha256.New, key)
	h.Write([]byte(param))

	expectedMAC := h.Sum(nil)
	return fmt.Sprintf("%x", expectedMAC)
}
